package com.wing.cms.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.cms.domain.Lookfeel;
import com.wing.cms.service.LookfeelManager;

/**
 * 
 * 
 * @author please  write your big name
 * 
 */
 
@Controller
@RequestMapping(value = "/cms/lookfeel")
public class LookfeelController {

	@Autowired
	private LookfeelManager lookfeelManager;
	
	/**
	 * 获得列表数据
	 *  
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "/cms/lookfeel/lookfeelIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String delete(@PathVariable String url) {
		return "/cms/lookfeel/"+url;
	}
		
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(int currentPage, String pageSize,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  lookfeelManager.findPageList(currentPage, _iPageSize,siteId);
	}
	
	/**
	 * 打开编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toForm/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable String id, Model model) {
		Lookfeel lookfeel = null;
		if(!id.equals("0")){
			lookfeel = lookfeelManager.findById(id);
		}else{
			lookfeel = new Lookfeel();
		}
		model.addAttribute("lookfeel", lookfeel);
		return "/cms/lookfeel/lookfeelForm";
	}
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public  @ResponseBody  String save(Lookfeel lookfeel,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		lookfeelManager.saveLookfeel(lookfeel,siteId);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	
	@RequestMapping(value = "dellookfeels", method = RequestMethod.POST)
	public @ResponseBody String dellookfeels(String[] chk) throws IOException {
		lookfeelManager.deleteLookfeels(chk);
		return ""+chk.length;
	}
}

